package com.evan.review.fragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

/**
 * 基于AlertDialog的DialogFragment
 * 演示最简单的警告对话框
 */
public class AlertDialogFragment extends DialogFragment {

    private static final String ARG_TITLE = "title";
    private static final String ARG_MESSAGE = "message";
    
    private AlertDialogListener listener;
    
    /**
     * AlertDialog回调接口
     */
    public interface AlertDialogListener {
        void onPositiveClick();
        void onNegativeClick();
    }
    
    /**
     * 创建实例的工厂方法
     */
    public static AlertDialogFragment newInstance(String title, String message, AlertDialogListener listener) {
        AlertDialogFragment fragment = new AlertDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putString(ARG_MESSAGE, message);
        fragment.setArguments(args);
        fragment.listener = listener;
        return fragment;
    }
    
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        String title = "提示";
        String message = "这是一个警告对话框";
        
        if (getArguments() != null) {
            title = getArguments().getString(ARG_TITLE, title);
            message = getArguments().getString(ARG_MESSAGE, message);
        }
        
        // 创建AlertDialog
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        builder.setTitle(title)
                .setMessage(message)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (listener != null) {
                            listener.onPositiveClick();
                        }
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (listener != null) {
                            listener.onNegativeClick();
                        }
                    }
                });
                
        return builder.create();
    }
} 